package hlhj.fhp.tvlib.activitys

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import hlhj.fhp.tvlib.R
import kotlinx.android.synthetic.main.activity_tv_aty.*
import com.shuyu.gsyvideoplayer.listener.LockClickListener
import kotlinx.android.synthetic.main.sample_video.view.*
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.ShowListBean
import hlhj.fhp.tvlib.utils.TimeUt
import java.util.*


class TvAty : BaseAty() {
    private var isPlay=false;
    private var dow=0;
    private var chooseTime=""
    private lateinit var loadingDialog:LoadingDialog
    private lateinit var  orientationUtils:OrientationUtils
    private lateinit var showListAdp:BaseQuickAdapter<ShowListBean.DataBean.ProgramBean,BaseViewHolder>//节目适配器
    private val showListDatas=ArrayList<ShowListBean.DataBean.ProgramBean>()
//    private var url="rtmp://live.hkstv.hk.lxdns.com/live/hks"
    private var url=""
    private var tittle="http://110.185.160.31:8433/zt/upload/video/20180526212045.mp4"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
        loadingDialog= LoadingDialog(this)
        url=intent.getStringExtra("source")
        tittle=intent.getStringExtra("tittle")
        setTime()
        setRcy()
        setPlayer()
        setSp()
        btExit.setOnClickListener {
            finish()
        }
    }

    override fun getContentId(): Int {
        return R.layout.activity_tv_aty
    }
    private fun setSp() {
        spView.header=DefaultHeader(this)
        spView.setListener(object :SpringView.OnFreshListener{
            override fun onLoadmore() {
            }

            override fun onRefresh() {
                showListDatas.clear()
                getDetailData()
            }
        })
    }

    private fun setRcy() {
        showListAdp=object :BaseQuickAdapter<ShowListBean.DataBean.ProgramBean,BaseViewHolder>(R.layout.tv_showlist_item,showListDatas){
            override fun convert(helper: BaseViewHolder?, item: ShowListBean.DataBean.ProgramBean?) {
                helper?.setText(R.id.itemTime,item?.time)
                helper?.setText(R.id.itemTittle,item?.name)

            }
        }
        recyclerview.adapter=showListAdp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)


    }

    private fun setTime() {
        val list = TimeUt.printWeekdays()
        val list2 = TimeUt.printWeekdays2()
        Log.e("fhp","日期1${list.toString()}日期2${list2}")
        btDay1.setOnClickListener{
            chooseTime=list2[0]
            getDetailData()
            tv1.setTextColor(resources.getColor(R.color.choose))
            tv_one.setTextColor(resources.getColor(R.color.choose))
            tv_one.setBackgroundResource(R.drawable.day_choose)

            tv2.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setBackgroundResource(R.drawable.day_unchoose)

            tv3.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setBackgroundResource(R.drawable.day_unchoose)

            tv4.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setBackgroundResource(R.drawable.day_unchoose)

            tv5.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setBackgroundResource(R.drawable.day_unchoose)

            tv6.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setBackgroundResource(R.drawable.day_unchoose)

            tv7.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setBackgroundResource(R.drawable.day_unchoose)
        }
        btDay2.setOnClickListener{
            chooseTime=list2[1]
            getDetailData()

            tv2.setTextColor(resources.getColor(R.color.choose))
            tv_two.setTextColor(resources.getColor(R.color.choose))
            tv_two.setBackgroundResource(R.drawable.day_choose)

            tv1.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setBackgroundResource(R.drawable.day_unchoose)

            tv3.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setBackgroundResource(R.drawable.day_unchoose)

            tv4.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setBackgroundResource(R.drawable.day_unchoose)

            tv5.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setBackgroundResource(R.drawable.day_unchoose)

            tv6.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setBackgroundResource(R.drawable.day_unchoose)

            tv7.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setBackgroundResource(R.drawable.day_unchoose)
        }
        btDay3.setOnClickListener{
            chooseTime=list2[2]
            getDetailData()
            tv3.setTextColor(resources.getColor(R.color.choose))
            tv_three.setTextColor(resources.getColor(R.color.choose))
            tv_three.setBackgroundResource(R.drawable.day_choose)

            tv1.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setBackgroundResource(R.drawable.day_unchoose)

            tv2.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setBackgroundResource(R.drawable.day_unchoose)

            tv4.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setBackgroundResource(R.drawable.day_unchoose)

            tv5.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setBackgroundResource(R.drawable.day_unchoose)

            tv6.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setBackgroundResource(R.drawable.day_unchoose)

            tv7.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setBackgroundResource(R.drawable.day_unchoose)
        }
        btDay4.setOnClickListener{
            getDetailData()
            chooseTime=list2[3]
            tv4.setTextColor(resources.getColor(R.color.choose))
            tv_four.setTextColor(resources.getColor(R.color.choose))
            tv_four.setBackgroundResource(R.drawable.day_choose)

            tv1.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setBackgroundResource(R.drawable.day_unchoose)

            tv2.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setBackgroundResource(R.drawable.day_unchoose)

            tv3.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setBackgroundResource(R.drawable.day_unchoose)

            tv5.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setBackgroundResource(R.drawable.day_unchoose)

            tv6.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setBackgroundResource(R.drawable.day_unchoose)

            tv7.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setBackgroundResource(R.drawable.day_unchoose)
        }
        btDay5.setOnClickListener{
            getDetailData()
            chooseTime=list2[4]
            tv5.setTextColor(resources.getColor(R.color.choose))
            tv_five.setTextColor(resources.getColor(R.color.choose))
            tv_five.setBackgroundResource(R.drawable.day_choose)

            tv1.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setBackgroundResource(R.drawable.day_unchoose)

            tv2.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setBackgroundResource(R.drawable.day_unchoose)

            tv3.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setBackgroundResource(R.drawable.day_unchoose)

            tv4.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setBackgroundResource(R.drawable.day_unchoose)

            tv6.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setBackgroundResource(R.drawable.day_unchoose)

            tv7.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setBackgroundResource(R.drawable.day_unchoose)
        }
        btDay6.setOnClickListener{
            chooseTime=list2[5]
            getDetailData()
            tv6.setTextColor(resources.getColor(R.color.choose))
            tv_six.setTextColor(resources.getColor(R.color.choose))
            tv_six.setBackgroundResource(R.drawable.day_choose)

            tv1.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setBackgroundResource(R.drawable.day_unchoose)

            tv2.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setBackgroundResource(R.drawable.day_unchoose)

            tv3.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setBackgroundResource(R.drawable.day_unchoose)

            tv4.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setBackgroundResource(R.drawable.day_unchoose)

            tv5.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setBackgroundResource(R.drawable.day_unchoose)

            tv7.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setTextColor(resources.getColor(R.color.unChosse))
            tv_seven.setBackgroundResource(R.drawable.day_unchoose)
        }
        btDay7.setOnClickListener{
            chooseTime=list2[6]
            getDetailData()
            tv7.setTextColor(resources.getColor(R.color.choose))
            tv_seven.setTextColor(resources.getColor(R.color.choose))
            tv_seven.setBackgroundResource(R.drawable.day_choose)

            tv1.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setTextColor(resources.getColor(R.color.unChosse))
            tv_one.setBackgroundResource(R.drawable.day_unchoose)

            tv2.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setTextColor(resources.getColor(R.color.unChosse))
            tv_two.setBackgroundResource(R.drawable.day_unchoose)

            tv3.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setTextColor(resources.getColor(R.color.unChosse))
            tv_three.setBackgroundResource(R.drawable.day_unchoose)

            tv4.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setTextColor(resources.getColor(R.color.unChosse))
            tv_four.setBackgroundResource(R.drawable.day_unchoose)

            tv5.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setTextColor(resources.getColor(R.color.unChosse))
            tv_five.setBackgroundResource(R.drawable.day_unchoose)

            tv6.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setTextColor(resources.getColor(R.color.unChosse))
            tv_six.setBackgroundResource(R.drawable.day_unchoose)
        }
        tv_one.text = list[0]
        tv_two.text = list[1]
        tv_three.text = list[2]
        tv_four.text = list[3]
        tv_five.text = list[4]
        tv_six.text = list[5]
        tv_seven.text = list[6]

        dow = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
        when(dow){
            1->{
                chooseTime=list2[6]
                btDay7.performClick()
            }
            2->{
                chooseTime=list2[0]
                btDay1.performClick()

            }
            3->{
                chooseTime=list2[1]
                btDay2.performClick()

            }
            4->{
                chooseTime=list2[2]
                btDay3.performClick()

            }
            5->{
                chooseTime=list2[3]
                btDay4.performClick()

            }
            6->{
                chooseTime=list2[4]
                btDay5.performClick()

            }
            7->{
                chooseTime=list2[5]
                btDay6.performClick()

            }

        }


    }

    private fun setPlayer() {
        gsyPlayer.setUp(url, false, tittle)
        gsyPlayer.startButton.performClick()

        orientationUtils = OrientationUtils(this, gsyPlayer)
        orientationUtils.isEnable = false
        //初始化不打开外部的旋转
        gsyPlayer.setIsTouchWiget(true);
        //关闭自动旋转
        gsyPlayer.setRotateViewAuto(false);
        gsyPlayer.setLockLand(false);
        gsyPlayer.setShowFullAnimation(false);
        gsyPlayer.setNeedLockFull(true);
        gsyPlayer.setVideoAllCallBack(object : GSYSampleCallBack() {
            override fun onPrepared(url: String?, vararg objects: Any?) {
                super.onPrepared(url, *objects)
                orientationUtils.isEnable = true
                isPlay = true
            }

            override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
                super.onQuitFullscreen(url, *objects)
                if (orientationUtils != null) {
                    orientationUtils.backToProtVideo()
                }
            }

            override fun onAutoComplete(url: String?, vararg objects: Any?) {
                super.onAutoComplete(url, *objects)
            }

            override fun onClickStartError(url: String?, vararg objects: Any?) {
                super.onClickStartError(url, *objects)
            }
        })
        gsyPlayer.setLockClickListener(object : LockClickListener {
            override fun onClick(view: View?, lock: Boolean) {
                if (orientationUtils != null) {
                    //配合下方的onConfigurationChanged
                    orientationUtils.isEnable = !lock
                }
            }
        })
        gsyPlayer.getTitleTextView().setVisibility(View.GONE);
        gsyPlayer.getBackButton().setVisibility(View.GONE);

        gsyPlayer.fullscreenButton.setOnClickListener {
            orientationUtils.resolveByClick()
            gsyPlayer.startWindowFullscreen(this@TvAty, false, true)
        }

        getDetailData()
    }

    private fun getDetailData() {
        OkGo.get<String>(Contents.BaseUrl + Contents.SHOW_LIST)
                .params("tv_id", intent.getIntExtra("tid", 0))
                .params("program_date", chooseTime)
                .execute(object : StringCallback() {
                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                        loadingDialog.show()
                    }
                    override fun onSuccess(response: Response<String>?) {
                        loadingDialog.dismiss()
                        spView.onFinishFreshAndLoad()
                        val str = response?.body()
                        val listBean = Gson().fromJson(str, ShowListBean::class.java)
                        if (listBean.code==1){
//                            gsyPlayer.setUp(listBean.data.tv_source, false, listBean.data.channel_name)
                            showListDatas.clear()
                            showListDatas.addAll(listBean.data.program)
                            showListAdp.notifyDataSetChanged()
                        }else{
                            Toast.makeText(this@TvAty,listBean.msg,Toast.LENGTH_SHORT).show()
                        }
                    }
                })
    }

    override fun onBackPressed() {
        if (orientationUtils!=null){
            orientationUtils.backToProtVideo()
        }
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed()

    }
    override fun onPause() {
        getCurPlay().onVideoPause()
        super.onPause()
    }

    override fun onResume() {
        getCurPlay().onVideoResume()
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        getCurPlay().release()
        if (orientationUtils!=null){
            orientationUtils.releaseListener()
        }
    }

    private fun getCurPlay(): GSYVideoPlayer {
        return if (gsyPlayer.fullWindowPlayer != null) {
            gsyPlayer.fullWindowPlayer
        } else gsyPlayer
    }
}
